<?php
namespace app\helppay\model;

use app\BaseModel;
use app\shop\model\AdminShopOrder;
use think\facade\Db;

class Helppay extends BaseModel
{
    //定义表名
    protected $name = 'longbing_card_shop_help_pay';




    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:04
     * @功能说明:添加
     */
    public function dataAdd($data){

        $data['create_time'] = time();

        $res = $this->insert($data);

        return $res;

    }


    /**
     * @author chenniang
     * @DataTime: 2021-06-25 17:34
     * @功能说明:退款
     */
    public function refund($id){

        $data = $this->dataInfo(['id'=>$id]);

        $payConfig = $this->payConfig($data['uniacid']);

        $response = orderRefundApi($payConfig,$data['pay_price'],$data['pay_price'],$data['transaction_id']);

        if ( isset( $response[ 'return_code' ] ) && isset( $response[ 'result_code' ] ) && $response[ 'return_code' ] == 'SUCCESS' && $response[ 'result_code' ] == 'SUCCESS' ) {

            $out_refund_no = $response[ 'out_refund_no' ];

            $this->dataUpdate(['id'=>$data['id']],['cancel_transaction_id'=>$out_refund_no,'status'=>0]);
        }

        return true;

    }


    /**
     * @author chenniang
     * @DataTime: 2021-06-26 17:56
     * @功能说明:退款普通订单
     */
    public function refundOrder($order){

        if(!empty($order['help_refund'])){

            return false;
        }

        $payConfig = $this->payConfig($order['uniacid']);

        $response = orderRefundApi($payConfig,$order['total_price'],$order['total_price'],$order['transaction_id']);


        if ( isset( $response[ 'return_code' ] ) && isset( $response[ 'result_code' ] ) && $response[ 'return_code' ] == 'SUCCESS' && $response[ 'result_code' ] == 'SUCCESS' ) {

            $out_refund_no = $response[ 'out_refund_no' ];

            $order_model = new AdminShopOrder();

            $order_model->orderUpdate(['id'=>$order['id']],['help_refund'=>$out_refund_no]);
        }

        return true;

    }


    /**
     *
     * 获取支付信息
     */
    public function payConfig ($uniacid = ''){
        $uniacid_id = !empty($uniacid)?$uniacid:$this->_uniacid;
        $pay    = Db::name('longbing_card_config_pay')->where(['uniacid'=>$uniacid_id])->find();
        $config = Db::name( 'longbing_card_config')->where(['uniacid' => $uniacid_id])->find();
        if(empty($pay[ 'mch_id' ])||empty($pay[ 'pay_key' ])){
            $this->errorMsg('未配置支付信息');
        }
        $setting[ 'payment' ][ 'merchant_id' ] = $pay[ 'mch_id' ];
        $setting[ 'payment' ][ 'key' ]         = $pay[ 'pay_key' ];
        $setting[ 'payment' ][ 'cert_path' ]   = $pay[ 'cert_path' ];
        $setting[ 'payment' ][ 'key_path' ]    = $pay[ 'key_path' ];
        $setting[ 'app_id' ]                   = $config['appid'];
        $setting[ 'secret' ]                   = $config['app_secret'];
        return $setting;
    }

    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:05
     * @功能说明:编辑
     */
    public function dataUpdate($dis,$data){

       // $data['update_time'] = time();

        $res = $this->where($dis)->update($data);

        return $res;

    }


    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:06
     * @功能说明:列表
     */
    public function dataList($dis,$page=10){

        $data = $this->where($dis)->order('top desc,id desc')->paginate($page)->toArray();

        return $data;

    }


    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:43
     * @功能说明:
     */
    public function dataInfo($dis){

        $data = $this->where($dis)->find();

        return !empty($data)?$data->toArray():[];

    }


    /**
     * @author chenniang
     * @DataTime: 2021-03-19 16:08
     * @功能说明:开启默认
     */
    public function updateOne($id){

        $user_id = $this->where(['id'=>$id])->value('user_id');

        $res = $this->where(['user_id'=>$user_id])->where('id','<>',$id)->update(['status'=>0]);

        return $res;
    }


    /**
     * @author chenniang
     * @DataTime: 2021-06-23 10:19
     * @功能说明:代付信息
     */
    public function helpList($order_id){

        $dis = [

            'a.order_id' => $order_id,

            'a.status'   => 1
        ];

        $data = $this->alias('a')
               ->join('longbing_card_user b','a.user_id = b.id')
                ->where($dis)
                ->field("a.*,b.nickName,b.avatarUrl,FROM_UNIXTIME(a.pay_time,'%Y-%m-%d %H:%i:%s') as pay_time")
                ->group('a.id')
                ->order('a.id desc')
                ->paginate()
                ->toArray();


        return $data;
    }





}